<?
/**************************************************************************************************
保存上传文件
Author			warmy
Create Date		2005-09-29

参数			form			设置存放上传文件名称表单的名称
				form			设置存放上传文件名称文本框名称
**************************************************************************************************/
require("../../config.php");
$form = "";
$formname = "";
if(isset($_GET["formname"]))
	$formname = $_GET['formname'];
if(isset($_GET['form']))
	$form = $_GET['form'];
if(isset($_POST['type']) &&  $_POST['type'] == "1")
	$type	=	1;
else 
	$type	= 0;
$path	= $_POST['filepath'];
if($path != "")
	$path	= "$path/";
function return_bytes($val) {
	$val = trim($val);
	$last = strtolower($val{strlen($val)-1});
	switch($last) {
		case 'g':
			$val *= 1024;
		case 'm':
			$val *= 1024;
		case 'k':
			$val *= 1024;
	}
	return $val;
}
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>文件上传</title>
</head>
<style type="text/css">
<!--
body {  font-size: 9pt}
a {  color: #000000; text-decoration: none}
a:hover {  text-decoration: underline}
.tx {  height: 16px; width: 30px; border-color: black black #000000; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 1px; border-left-width: 0px; font-size: 9pt; background-color: #eeeeee; color: #0000FF}
.bt {  font-size: 9pt; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; height: 16px; width: 80px; background-color: #eeeeee; cursor: hand}
.tx1 { height: 20px; width: 30px; font-size: 9pt; border: 1px solid; border-color: black black #000000; color: #0000FF}
-->
</style>
<body>
<?php
if ($formname != "" && $form != "")
{
	$upload_max_filesize = return_bytes(ini_get("upload_max_filesize"));
	$u = new WUploadFile(mb_convert_encoding($_FILES[$formname]['name'],SYSTEM_CODE,WEB_CODE),
	 		mb_convert_encoding($_FILES[$formname]['tmp_name'],SYSTEM_CODE,WEB_CODE), 
	 		$_FILES[$formname]['size'],0 );
	 		
	$para	= array("TYPE"		=> "gif,jpg,jpeg",
					"SIZE"		=> $upload_max_filesize,
					"EXISTS"	=> 0,
					"RENAME"	=> 0,
					"ERROR"		=>1,
					"RESIZE"	=>"");
	if (isset($_COOKIE["{$_POST['filepath']}_{$_GET['formname']}_UPLOAD"])) 
	{
		$coo = $_COOKIE["{$_POST['filepath']}_{$_GET['formname']}_UPLOAD"];
		$tmp	= explode("|",$coo);
		$para['TYPE'] = $tmp[0];
    	$para['SIZE'] = $tmp[1];
    	$para['EXISTS'] = $tmp[2];
    	$para['RENAME'] = $tmp[3];
    	$para['ERROR'] = $tmp[4];
    	if(isset($tmp[5])) $para['RESIZE'] = $tmp[4];
		
//		foreach ($_COOKIE["{$_POST['filepath']}_{$_GET['formname']}_UPLOAD"] as $name => $value) 
//			$para[$name]	= $value;
	}
	elseif (isset($_COOKIE["{$_POST['filepath']}_UPLOAD"])) 
	{
		$coo = $_COOKIE["{$_POST['filepath']}_UPLOAD"];
		$tmp	= explode("|",$coo);
		$para['TYPE'] = $tmp[0];
    	$para['SIZE'] = $tmp[1];
    	$para['EXISTS'] = $tmp[2];
    	$para['RENAME'] = $tmp[3];
    	$para['ERROR'] = $tmp[4];
    	if(isset($tmp[5])) $para['RESIZE'] = $tmp[4];
//		foreach ($_COOKIE["{$_POST['filepath']}_UPLOAD"] as $name => $value) 
//			$para[$name]	= $value;
	}
	$u->UPLOAD_FILE_TYPE 				= $para['TYPE'];
	$u->UPLOAD_FILE_MAX_SIZE 			= $para['SIZE']>$upload_max_filesize?$upload_max_filesize:$para['SIZE'];
	$u->UPLOAD_FILE_CHECK_FILE_EXISTS	= $para['EXISTS'];
	$u->UPLOAD_FILE_AUTO_RENAME			= $para['RENAME'];
	$u->UPLOAD_FILE_ERROR_TYPE			= $para['ERROR'];

	$result = $u->upload(UPLOAD_FILE_PATH.$path,$type);
	$water_mark = defined("WATERMARK")?1:0;
	$water_para = isset($WATER_PARA)?$WATER_PARA:array();
	$WImage = new WImage($water_mark,$water_para);
	if($result == "")
	{
		//图片上传完成，确实是否有图片需要切
		if($para['RESIZE'] != "")
		{
			$tmp = explode("*",$para['RESIZE']);
			
			for($i=0;$i<count($tmp);$i=$i+3)
			{
				if($i+3>count($tmp))
					break;
				$ext		= pathinfo($u->URL);
				if($tmp[$i+2] == 3)
				{
					$tmp[$i+2] = 2;
					$filename 	= substr($u->URL,0,strlen($u->URL)-strlen($ext['extension'])-1);
				}
				else 
				{
					$filename 	= substr($u->URL,0,strlen($u->URL)-strlen($ext['extension'])-1) . "_$i";
				}
				$WImage->ResizeImage(DOCUMENTROOT.$u->URL,$tmp[$i],$tmp[$i+1],$tmp[$i+2]);
				if(defined("WATERMARK"))
				{
					$WImage->Water(realpath(DOCUMENTROOT.$u->URL));
					$WImage->Save($filename);
				}
				$WImage->Save(DOCUMENTROOT.$filename);
			}
		}
		else 
		{
			if(defined("WATERMARK"))
			{
				$filename = DOCUMENTROOT.$u->URL;
				$WImage->Water($filename);
				$filename = substr($filename,0,strrpos($filename,"."));
				$WImage->Save($filename);
			}
		}
		$url	= mb_convert_encoding($u->URL,WEB_CODE,SYSTEM_CODE);
		print("<script>window.opener.document.$form.$formname.value='$url';\r\n");
		print("window.alert(\"文件上传成功!请不要修改生成的链接地址！\");\r\n");
		print("var obj = window.opener.GETOBJ('upload_view_$formname');\r\n");
		
		$name	= mb_convert_encoding($u->FileName,WEB_CODE,SYSTEM_CODE);
		print("obj.innerHTML='<a href=\"$url\" target=\"_blank\">$name</a>';\r\n");
		print("window.close();\n");
		print("</script>\n");
		//增加附件纪录
		$active = date("Y-m-d H:i:s");
		$sql = "insert into ".WWCMSATTACHETABLE."(`catalog`,`src`,`active`) values('','$url','$active')";
		$connectid = mysql_connect(DB_HOST,DB_USER,DB_PWD); 
		mysql_select_db(DB_NAME);
		mysql_query($sql);
	}
	else
	{
		echo $result;
	}
}
else
	print("未设置表单名称");
?>

</body>
</html>
